﻿using System.ComponentModel;
using System.Runtime.Serialization;
using System.Xml.Serialization;

using GameLibrary.Internal;

namespace G.Model
{
    [DataContract]
    public class GamePlayerDef : 
        System.IEquatable<GamePlayerDef>,
        System.IComparable<GamePlayerDef>,
        HasGameSpaceDef, HasName 
    {
        private GameSpaceDef _Game;
        
        [DataMember(Name = "Name")]
        private string _Name;
        
        [DataMember(Name = "Color")]
        private string _ColorString;
        private System.Drawing.Color _Color;

        public GamePlayerDef() : this(null, "") {}
            
        public GamePlayerDef(GameSpaceDef game, string name)
        {
            _Game = game;
            _Name = name;
            Color = System.Drawing.Color.Turquoise;
        }

        [Browsable(false), XmlIgnore, IgnoreDataMemberAttribute]
        public GameSpaceDef GameDef
        {
            get { return _Game; }
            set { _Game = value; }
        }

        public string Name
        {
            get { return _Name; }
        }

        [IgnoreDataMember]
        public System.Drawing.Color Color
        {
            get { return _Color; }
            set 
            {
                if (_Color != value)
                {
                    _Color = value;
                    if (value.IsKnownColor)
                    {
                        _ColorString = value.ToKnownColor().ToString();
                    }
                    else
                    {
                        _ColorString = value.ToArgb().ToString();
                    }
                    if (_Game != null)
                    {
                        _Game.GamePlayerDefs.FirePropertyChanged(this);
                    }
                }
            }
        }

        [Browsable(false), IgnoreDataMember]
        public GameSpaceDef Game
        {
            get { return _Game; }
        }

        public int CompareTo(GamePlayerDef other)
        {
            return this.Name.CompareTo(other.Name);
        }

        public bool Equals(GamePlayerDef other)
        {
            return (other.Name == this._Name);
        }

        public override bool Equals(object obj)
        {
            if (obj is GamePlayerDef)
            {
                GamePlayerDef other = (GamePlayerDef)obj;
                return (other.Name == this._Name);
            }
            return base.Equals(obj);
        }

        public override int GetHashCode()
        {
            return _Name.GetHashCode();
        }

        public override string ToString()
        {
            return this._Name;
        }

    }
}
